package web2021.team1.lj.service;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;
import web2021.team1.lj.dao.LoginLogDao;
import web2021.team1.lj.dao.UserDao;
import web2021.team1.lj.domain.LoginLog;
import web2021.team1.lj.domain.User;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hasMatchUser(String username, String password) {
        int matchCount = userDao.getMatchCount(username, password);
        return matchCount > 0;
    }

    public User findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }


    public void loginSuccess(User user) {
        System.out.println("成功进入loginSuccess");
//        LoginLog loginLog = new LoginLog();
//        loginLog.setUserId(user.getUserId());
//        loginLog.setIp(user.getLastIp());
//        loginLog.setLoginDate(user.getLastVisit());
//        loginLogDao.insertLoginLog(loginLog);
//        userDao.updateLoginInfo(user);
    }

    @Transactional
    public boolean registerUser(String username, String password) {
        //先判断一下是否有匹配的用户
        User user = findUserByUserName(username);
        System.out.println(user);
        //user == null 说明 之前数据库无此用户名的用户
        if(user.getUserName() == null) {
            System.out.println("用户名可用");
            userDao.creatUser(username, password);
            return true;
        }else {
            System.out.println("用户名不可用");
            return false;
        }
    }


    @Test
    public void testRegister(){
        UserService userService = new UserService();
        String username = "admin";
        String password = "test";
        boolean isSuccess =userService.registerUser(username,password);
        System.out.println(isSuccess);
    }
}
